package com.relytosoft.mizar.analyze.structure.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.structure.api.entity.AyStructureDisposition;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyStructureDispositionDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyTaskIdDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.query.AyStructureDispositionQuery;
import com.relytosoft.mizar.analyze.structure.api.pojo.vo.AyStructureDispositionVo;
import com.relytosoft.mizar.analyze.structure.services.service.AyStructureDispositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: zyc
 * @version: v1.0
 * @description: com.relytosoft.mizar.analyze.structure.services.controller
 * @Date: 2022/5/18 15:21
 */
@Api(tags = {"结构化布控任务"})
@RestController
@RequestMapping("/structure/disposition")
public class AyStructureDispositionController {
    @Resource
    private AyStructureDispositionService ayStructureDispositionService;


    @ApiOperation("查询结构化布控任务列表")
    @GetMapping({"/list"})
    public ResponseResult<Map<String, Object>> select(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        AyStructureDispositionQuery ayStructureDispositionQuery = JSON.parseObject(bodys, AyStructureDispositionQuery.class);
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        IPage<AyStructureDispositionVo> page = ayStructureDispositionService.selectAyStructureDispositionPageList(pageQuery, ayStructureDispositionQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("修改结构化布控任务")
    @PutMapping({"/update"})
    public ResponseResult<Void> update(@RequestBody AyStructureDispositionDto ayStructureDispositionDto) {
        boolean update = ayStructureDispositionService.updateAyStructureDisposition(ayStructureDispositionDto);
        if (update) {
            return ResponseResult.ok("修改结构化布控任务成功");
        }
        return ResponseResult.fail("修改结构化布控任务失败");
    }


    @ApiOperation("创建结构化布控任务")
    @PostMapping({"/insert"})
    public ResponseResult<Void> insert(@RequestBody AyStructureDispositionDto ayStructureDispositionDto) {
        boolean insert = ayStructureDispositionService.insertAyStructureDisposition(ayStructureDispositionDto);
        if (insert) {
            return ResponseResult.ok("创建结构化布控任务成功");
        }
        return ResponseResult.fail("创建结构化布控任务失败");
    }


    @ApiOperation("删除结构化布控任务")
    @DeleteMapping({"/delete"})
    public ResponseResult<Void> delete(@RequestBody List<Integer> list) {
        boolean delete = ayStructureDispositionService.deleteAyStructureDisposition(list);
        if (delete) {
            return ResponseResult.ok("已删除符合条件的结构化布控任务");
        }
        return ResponseResult.fail("没有符合删除条件的结构化布控任务");
    }


    @ApiOperation("启动结构化布控任务")
    @PostMapping({"/start/{id}"})
    public ResponseResult<Void> start(@PathVariable Integer id) {
        boolean start = ayStructureDispositionService.startStructureDisposition(id);
        if (start) {
            return ResponseResult.ok("启动结构化布控任务成功");
        }
        return ResponseResult.fail("启动结构化布控任务失败");
    }


    @ApiOperation("停止结构化布控任务")
    @PostMapping({"/stop/{id}"})
    public ResponseResult<Void> stop(@PathVariable Integer id) {
        boolean stop = ayStructureDispositionService.stopStructureDisposition(id);
        if (stop) {
            return ResponseResult.ok("停止结构化布控任务成功");
        }
        return ResponseResult.fail("停止结构化布控任务失败");
    }


    @ApiOperation("布防/撤防")
    @PostMapping({"/defense"})
    public ResponseResult<Void> defense(@RequestBody AyTaskIdDto ayTaskIdDto) {
        boolean status = ayStructureDispositionService.armedAndDisarmed(ayTaskIdDto);
        if (status) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }


    @ApiOperation("设置临时车辆库白名单")
    @PostMapping({"/temp"})
    public ResponseResult<Void> updateTempVehicleGroup(@RequestBody AyStructureDispositionDto ayStructureDispositionDto) {
        boolean update = ayStructureDispositionService.updateTempVehicleGroup(ayStructureDispositionDto);
        if (update) {
            return ResponseResult.ok("设置车辆库临时白名单成功");
        }
        return ResponseResult.fail("设置车辆库临时白名单失败");
    }

    @ApiOperation("结构化布控任务详细信息")
    @GetMapping({"/getInfo/{dispositionId}"})
    public ResponseResult get(@PathVariable Integer dispositionId) {
        AyStructureDisposition ayStructureDisposition = ayStructureDispositionService.getAyStructureDispositionById(dispositionId);
        return ResponseResult.ok(ayStructureDisposition);
    }
}
